<template>
  <div class="container">
    <div class="main">
      <h2 class="title">注册</h2>
      <el-form :model="form" ref="formRef" :rules="rules" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" clearable>
            <template #prefix>
              <span style="color: #666;width:40px;">用户名</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="密码" type="password" clearable>
            <template #prefix>
              <span style="color: #666;width:40px">密码</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password2">
          <el-input v-model="form.password2" placeholder="确认密码" type="password" clearable>
            <template #prefix>
              <span style="color: #666;width:40px">密码</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
        </el-form-item>

        <el-form-item>
          <el-link type="info" @click="$router.push('/login')">已有账号？登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>


<script setup>
import {reactive, ref} from 'vue'
import {ElMessage} from 'element-plus'
import {register} from '@/api/index.js'
import {useRouter} from 'vue-router' // 添加这行引入
import {useUserStore} from '@/stores/user.js'

const userStore = useUserStore()

const router = useRouter()
const formRef = ref(null)

const form = reactive({
  username: '',
  password: '',
  password2: '',
})
const rules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 2, message: '用户名至少2个字符', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, message: '密码至少6位', trigger: 'blur'}
  ],
  password2: [
    {required: true, message: '请确认密码', trigger: 'blur'},
    {
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const handleRegister = async () => {
  try {
    const valid = await formRef.value.validate()
    if (!valid) return

    await userStore.userRegister({
      username: form.username,
      password: form.password,
      password2: form.password2,
    })
    ElMessage.success('注册成功，请重新登录')
    router.push('/login') // 注册成功后跳转到登录页
  } catch (error) {
    console.error('注册失败:', error)
    let errorMessage = '注册失败，请重试'
    if (error.response) {
      errorMessage = error.response.data.detail ||
          error.response.data.message ||
          errorMessage
    } else if (error.request) {
      errorMessage = '网络错误，请检查连接'
    }
    ElMessage.error(errorMessage)
    form.password = ''
    form.password2 = ''
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: linear-gradient(-45deg, #4facfe, #00f2fe, #4facfe, #00f2fe);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;

}

.main {
  background-color: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
  width: 360px;
  margin-top: 10vh;
}

.title {
  text-align: center;
  margin: 0 0 30px;
  font-size: 22px;
  color: skyblue;
}

@keyframes gradientBG {
  0% {
    background-position: 0 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 50%;
  }
}
</style>
